﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Moq;
using Xrm.Solutions.FrameworkSample.Common;
using Xrm.Solutions.FrameworkSample.Plugins;
using Xrm.Framework.Test.Unit.Moq;


namespace Xrm.Solutions.FrameworkSample.XrmUnitTests.Plugins
{
    [TestClass]
    public class TestRunIdentityCheckUsingMoq : PluginUnitTest
    {
        #region Instance Variables

        //TODO: Declare your variables for setup and verification
        private int score = 0;
        private bool updateCalled = false;

        #endregion

        private TestContext context;

        public TestContext TestContext
        {
            get { return context; }
            set { context = value; }
        }

        #region Setup

        protected override IPlugin SetupPlugin()
        {
            //TODO: Setup your trigger
            base.SetPluginEvent("xdft_identitycheck", "Create",
               Xrm.Framework.Test.Unit.SdkMessageProcessingStepImage.PostOperation);

            //TODO: Setup your Moqs

            string firstname = context.DataRow[0].ToString();
            string lastname = context.DataRow[1].ToString();
            DateTime dob = (DateTime)context.DataRow[2];
            string address = context.DataRow[3].ToString();
            score = Int32.Parse(context.DataRow[4].ToString());

            Entity identityCheck = new Entity("xdft_identitycheck");
            identityCheck["xdft_firstname"] = firstname;
            identityCheck["xdft_lastname"] = lastname;
            identityCheck["xdft_dateofbirth"] = dob;
            identityCheck["xdft_address"] = address;
            identityCheck["xdft_contactid"] = new EntityReference("contact", new Guid());

            base.SetTarget(identityCheck);

            Mock<IIdentityCheckService> identityServiceMock = new Mock<IIdentityCheckService>();

            identityServiceMock.Setup(service => service.RunCheck(firstname,
                                                    lastname,
                                                    dob,
                                                    address)
                            ).Returns(score);

            Xrm.Solutions.FrameworkSample.Common.Fakes.ShimIdentityCheckService.AllInstances.RunCheckStringStringDateTimeString =
                (service, fn, ln, d, a) =>
                {
                    return identityServiceMock.Object.RunCheck(fn, ln, d, a);
                };

            base.OrganizationServiceMock.Setup(service => service.Update(It.IsAny<Entity>())).Callback<Entity>((Entity e) =>
            {
                int returnedScore = (int)e["xdft_identityscore"];
                if (returnedScore == score)
                    updateCalled = true;
            }
            );

            //TODO: Create your Plugin
            IPlugin plugin = new RunIdentityCheck();

            return plugin;
        }

        #endregion

        #region Test

        [TestMethod]
        [DeploymentItem("Customers.xlsx")]
        [DataSource("System.Data.Odbc",
            "Dsn=Excel Files;Driver={Microsoft Excel Driver(*.xlsx)};dbq=|DataDirectory|\\Customers.xlsx;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5;readonly=true",
            "Customers$",
            DataAccessMethod.Sequential)]
        public void RunTestRunIdentityCheckUsingMoq()
        {
            using (ShimsContext.Create())
            {
                base.Test();
            }
        }

        #endregion

        #region Verify

        protected override void Verify()
        {
            Assert.IsNull(Error);

            Assert.IsTrue(updateCalled);
        }

        #endregion
    }
}
